package com.shawn.spring.boot.mybatis.service.impl;

import com.shawn.spring.boot.mybatis.entity.Role;
import com.shawn.spring.boot.mybatis.entity.RoleExample;
import com.shawn.spring.boot.mybatis.mapper.RoleMapper;
import com.shawn.spring.boot.mybatis.service.RoleService;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shawn
 * @since 2020/8/30
 */
@Service
public class RoleServiceImpl implements RoleService {

  @Autowired private RoleMapper roleMapper;

  @Override
  public void add(Role role) {
    Date now = Calendar.getInstance().getTime();
    role.setCreateTime(now);
    role.setUpdateTime(now);
    role.setIsDeleted(0);
    int i = roleMapper.insert(role);
    roleMapper.insertRoleMenu(i, 1);
  }

  @Override
  public Role findById(long id) {
    return roleMapper.selectByPrimaryKey(id);
  }

  @Override
  public List<Role> findByUserId(long userId) {
    return roleMapper.selectByUserId(userId);
  }

  @Override
  public List<Role> findByName(String name) {
    RoleExample example = new RoleExample();
    example.createCriteria().andRoleNameLike(name);
    return roleMapper.selectByExample(example);
  }
}
